#!/bin/bash -e

# GPU Test Script (glmark2-es2-wayland)
# 支持参数：
#   -l          循环模式
#   -d N        每轮测试间隔（默认60秒）
#   -h          显示帮助信息

LOOP=false
DURATION=60
LOG_DIR="$HOME/stability-test-log"
LOG_FILE="$LOG_DIR/glmark.log"
mkdir -p "$LOG_DIR"

while getopts "ld:h" opt; do
    case $opt in
        l)
            LOOP=true
            ;;
        d)
            DURATION=$OPTARG
            ;;
        h)
            echo "Usage: $0 [-l] [-d SECONDS]"
            echo
            echo "Options:"
            echo "  -l          Loop mode (run glmark2 in loop)"
            echo "  -d SECONDS  Interval between loops (default: 60)"
            exit 0
            ;;
        \?)
            exit 1
            ;;
    esac
done

# 检查是否为 Bianbu Linux
if grep "Bianbu Linux" /etc/issue > /dev/null 2>&1; then
    BL=true
else
    BL=false
fi

# 安装 glmark2
if ! $BL; then
    command -v glmark2-es2-wayland > /dev/null || (sudo apt update && sudo apt install -y glmark2-es2-wayland)
fi

# 启动测试
echo "Starting GPU test..."
rm -f "$LOG_FILE"

if $LOOP; then
    while true; do
        echo "[`date`] Running glmark2..." >> "$LOG_FILE"
        glmark2-es2-wayland >> "$LOG_FILE" 2>&1
        echo "[`date`] Sleeping for $DURATION seconds..." >> "$LOG_FILE"
        sleep "$DURATION"
    done &
else
    glmark2-es2-wayland --run-forever >> "$LOG_FILE" 2>&1 &
fi

echo "GPU test started, log at: $LOG_FILE"

